package com.zhongkun.pojo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ToolUtil {

    //通过id在图库中查询与之匹配的pics
    public List<Picture> getPicById(int id, List<Picture> picture){
        List<Picture> pics = picture.stream().filter(p->p.getId() == id).collect(Collectors.toList());
        if (pics.size() == 0 || pics == null){
            System.out.println("这图有问题啊");
            return null;
        }
        return pics;
    }

    //通过id在用户组中查询与之匹配的users
    public List<User> getUserById(int id,List<User> userList){
        List<User> users = userList.stream().filter(u->u.getId() == id).collect(Collectors.toList());
        if (users.size() == 0 || users == null){
            System.out.println("这用户有问题啊");
            return null;
        }
        return users;
    }

    //通过id查询评论组中与之匹配的comments
    public List<Comment> getShareCommentById(int id,List<Comment> commentList) {
        List<Comment> comments = commentList.stream().filter(c -> c.getUser_id() == id).collect(Collectors.toList());
        if (comments.size() == 0 || comments == null) {
            System.out.println("没评论啊");
            return null;
        }
        return comments;
    }


    //通过id查询与之匹配的所有userShares
    public List<UserShare> getUserShareById(int id,List<UserShare> userShareList){
        List<UserShare> userShares = userShareList.stream().filter(us->us.getId() == id).collect(Collectors.toList());
        if (userShares.size() == 0 || userShares == null){
            System.out.println("这动态有问题啊");
            return null;
        }
        return userShares;
    }

    //生成随机九个序号 为pictureList 随机挑选九个id 返回每个id所指示的path
    public String[] getNinePictureByRandom(List<Picture> pictureList){
        List<Picture> picture =  new ArrayList<>();
        picture.addAll(pictureList);
        int maxId = picture.size();
        int theId = 0;
        int i = 0;
        String [] ninePicPath = new String[9];
        while (i < 9){
            theId = (int)(Math.random()*maxId);
            if(picture.isEmpty()) {
                i++;
                continue;
            }else if (i > maxId && maxId == 0){
                break;
            }
            ninePicPath[i] = picture.get(theId).getPath();
            picture.remove(theId);
            i++;
            maxId--;
        }
        return ninePicPath;
    }

}
